package com.dense.kuiniu.bookborrow.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.dense.kuiniu.bookborrow.business.event.USBChangeEvent;

import org.greenrobot.eventbus.EventBus;

/**
 * @author Administrator
 * @date 2021/1/27
 */
public class USBChangeReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        if (intent.getAction() != null && usbDevice != null) {
            Log.e("onReceive",usbDevice.getDeviceName());
            switch (intent.getAction()) {
                case UsbManager.ACTION_USB_DEVICE_ATTACHED: // 插入USB设备
                    EventBus.getDefault().post(new USBChangeEvent(usbDevice.getProductId(), usbDevice.getVendorId(), true));
                    break;
                case UsbManager.ACTION_USB_DEVICE_DETACHED: // 拔出USB设备
                    EventBus.getDefault().post(new USBChangeEvent(usbDevice.getProductId(), usbDevice.getVendorId(), false));
                    break;
                default:
                    break;
            }

        }
    }
}